package com.thprojects.myphotomemories.view.components;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class DrawableImageView extends ImageView implements OnTouchListener {

	final float mStroke = 5f;
    List<Draw> mDrawings = new ArrayList<Draw>();
    Draw mCurDrawing;
    Paint mPaint = new Paint();
	int mCurColor = Color.rgb(255, 0, 0);
	OnDrawListener mOdl;
	Rect mBoundingRect;

    public DrawableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);
        
        this.setOnTouchListener(this);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mStroke);
    }
    
    public void setOnDrawListener(OnDrawListener odl){
    	mOdl = odl;
    }
    
    public void setColor(int color){
    	mCurColor = color;
    }
    
    public void setBoundingRect(Rect r){
    	mBoundingRect = r;
    }
    
    public void undo(){
    	if (mDrawings.size() == 0) return;
    	mDrawings.remove(mDrawings.size()-1);
    	invalidate();
    }

    public void clear(){
    	mDrawings = new ArrayList<Draw>();
    	invalidate();
    }
    
    public boolean isClean(){
    	return mDrawings.size() == 0;
    }
    
    public Bitmap drawOriginalBitmap(Bitmap bmp, float bmpRatio, int dx, int dy){
    	Bitmap res = bmp.copy(Bitmap.Config.ARGB_8888, true);
    	Paint paint = new Paint();
    	paint.setAntiAlias(true);
    	paint.setStrokeWidth(mStroke*bmpRatio);
    	
    	Canvas canvas = new Canvas(res);
    	Log.d("MPM", "c.w="+canvas.getWidth()+", c.h="+canvas.getHeight());
    	Log.d("MPM", "bmpRatio = "+bmpRatio);
    	
    	for (Draw draw : mDrawings) {
    		paint.setColor(draw.mColor); 
        	
        	if (draw.mPoints.size() == 0) continue;
        	
        	//first point transformed
        	Point p = new Point();
        	p.x = (draw.mPoints.get(0).x - dx)*bmpRatio;
        	p.y = (draw.mPoints.get(0).y - dy)*bmpRatio;
        	
        	canvas.drawCircle(p.x, p.y, mStroke*bmpRatio/2, paint);
        	
        	for (int i=1; i<draw.mPoints.size(); i++){
        		//Point p1 = draw.mPoints.get(i-1);
        		Point p1 = new Point();
        		p1.x = (draw.mPoints.get(i-1).x - dx)*bmpRatio;
            	p1.y = (draw.mPoints.get(i-1).y - dy)*bmpRatio;
        		//Point p2 = draw.mPoints.get(i);
            	Point p2 = new Point();
            	p2.x = (draw.mPoints.get(i).x - dx)*bmpRatio;
            	p2.y = (draw.mPoints.get(i).y - dy)*bmpRatio;
        		canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint);
        		canvas.drawCircle(p2.x, p2.y, mStroke*bmpRatio/2, paint);
        	}
        }

    	
    	return res;
    }
    
    @Override
    public void onDraw(Canvas canvas) {
    	super.onDraw(canvas);
    	canvas.clipRect(mBoundingRect);
    	for (Draw draw : mDrawings) {
    		mPaint.setColor(draw.mColor); 
        	
        	if (draw.mPoints.size() == 0) continue;
        	
        	canvas.drawCircle(draw.mPoints.get(0).x, draw.mPoints.get(0).y, mStroke/2, mPaint);
        	
        	for (int i=1; i<draw.mPoints.size(); i++){
        		Point p1 = draw.mPoints.get(i-1);
        		Point p2 = draw.mPoints.get(i);
        		canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mPaint);
        		canvas.drawCircle(p2.x, p2.y, mStroke/2, mPaint);
        	}
        }
    }
    

    public boolean onTouch(View view, MotionEvent event) {
    	if(event.getAction() == MotionEvent.ACTION_DOWN){
    		mCurDrawing = new Draw(mCurColor);
    		mDrawings.add(mCurDrawing);
    		mOdl.OnDraw(); 
    	}
    	addPoint(event);
        return true;
    }
    
    private void addPoint(MotionEvent event){
    	Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        mCurDrawing.mPoints.add(point);
        invalidate();
    }
}

class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

class Draw{
	int mColor;
	List<Point> mPoints;
	
	Draw(int color){
		this.mColor = color;
		mPoints = new ArrayList<Point>();
	}
}
